package com.sam.jifenbao;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TabHost;
import com.sam.adapter.InvalidCouponListAdapter;
import com.sam.adapter.ValidCouponListAdapter;
import com.sam.entity.CouponEntity;
import java.util.ArrayList;
import java.util.List;

public class CouponListActivity extends BaseHeaderActivity {
    private TabHost tabHost;
    private ListView validCouponListView,invalidCouponListView;
    final List<CouponEntity> validCouponList = new ArrayList<>();
    final List<CouponEntity> invalidCouponList = new ArrayList<>();

    private static final String VALID_COUPON_TAB_TAG = "valid";
    private static final String INVALID_COUPON_TAB_TAG = "invalid";

    public static final String KEY_COUPON_DATA = "coupon";
    private int positionToSend;
    private ValidCouponListAdapter validCouponListAdapter;
    private InvalidCouponListAdapter invalidCouponListAdapter;
    private static final int REQUEST_USE_COUPON_CODE = 100;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_list);
        findView();
        initView();
    }

    @Override
    protected void findView() {
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        validCouponListView = (ListView) findViewById(R.id.valid_coupon_listview);
        invalidCouponListView = (ListView) findViewById(R.id.invalid_coupon_listview);
    }

    @Override
    protected void initView() {
        getHeaderView().setTitleText(R.string.shop_coupons);
        tabHost.setup();
        final String validStr = getString(R.string.valid);
        final String invalidStr = getString(R.string.invalid);
        tabHost.addTab(tabHost.newTabSpec(VALID_COUPON_TAB_TAG).setContent(R.id.valid_coupon_listview).setIndicator(validStr));
        tabHost.addTab(tabHost.newTabSpec(INVALID_COUPON_TAB_TAG).setContent(R.id.invalid_coupon_listview).setIndicator(invalidStr));

        for(int i = 0; i <20; i++) validCouponList.add(new CouponEntity(5+i,"新店沙拉套餐优惠券-"+i,
                10+i,System.currentTimeMillis()-1000000000L, System.currentTimeMillis()));
        validCouponListAdapter = new ValidCouponListAdapter(this, validCouponList);
        validCouponListView.setAdapter(validCouponListAdapter);
        validCouponListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(CouponListActivity.this,CouponDetailActivity.class);
                positionToSend = position;
                intent.putExtra(KEY_COUPON_DATA,validCouponList.get(position));
                startActivityForResult(intent,REQUEST_USE_COUPON_CODE);
            }
        });

        for(int i = 0; i <10; i++) invalidCouponList.add(new CouponEntity(10+i,"掉漆茶杯-"+i,
                10+i,System.currentTimeMillis()-1000000000L, System.currentTimeMillis()));
        invalidCouponListAdapter = new InvalidCouponListAdapter(this, invalidCouponList);
        invalidCouponListView.setAdapter(invalidCouponListAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_USE_COUPON_CODE && resultCode == RESULT_OK){
            CouponEntity couponEntity = validCouponList.get(positionToSend);
            couponEntity.setUsed(true);
            invalidCouponList.add(couponEntity);
            invalidCouponListAdapter.notifyDataSetChanged();

            validCouponList.remove(positionToSend);
            validCouponListAdapter.notifyDataSetChanged();
        }
    }
}
